package com.applications.codemash.utility;

import java.util.HashMap;
import java.util.Map;

import com.applications.codemash.core.GrouperBase;
import com.applications.codemash.core.IGetKey;
import com.applications.codemash.core.SessionCollection;
import com.applications.codemash.core.SpeakerCollection;
import com.applications.codemash.database.SessionStartDbAdapter;
import com.applications.codemash.groupers.SessionKeyForDate;
import com.applications.codemash.groupers.SessionListGroupByDay;

public final class CustomContext
{
	private Map<String, Object> _cache;
	@SuppressWarnings("unchecked")
	private <T> T getItem(String key)
	{
		if (_cache.containsKey(key))
		{
			return (T)_cache.get(key);
		}
		
		return null;
	}
	
	private <T> void setItem(String key, T item)
	{
		_cache.put(key, item);
	}
	
	private CustomContext()
	{
		_cache = new HashMap<String, Object>();
	}
	
	private static CustomContext _instance;
	
	private static CustomContext getCurrentContext()
	{
		if (_instance == null)
			_instance = new CustomContext();
		
		return _instance;
	}
	
	/* Methods getting/setting real values */
	private static final String SESSION_CACHE_KEY = "SessionCache";
	private static final String SPEAKER_CACHE_KEY = "SpeakerCache";
	private static final String ADAPTER_KEY = "Adapter";
	
	public static SessionCollection getSessionCache()
	{
		try
		{
			SessionCollection result = CustomContext.getCurrentContext().getItem(SESSION_CACHE_KEY);
			if (result == null || result.size() == 0)
			{
				setSessionCache(SessionCollection.GetAllSessions());
			}
			
			return result;
		}
		catch (Exception ex)
		{
			throw new RuntimeException();
		}
	}
	
	private static void setSessionCache(SessionCollection map)
	{
		CustomContext.getCurrentContext().setItem(SESSION_CACHE_KEY, map);
	}
	
	public static SpeakerCollection getSpeakerCache()
	{
		SpeakerCollection result = CustomContext.getCurrentContext().getItem(SPEAKER_CACHE_KEY);
		if (result == null)
		{
			setSpeakerCache(SpeakerCollection.GetAllSpeakers());
		}
		
		return result;
	}
	
	private static void setSpeakerCache(SpeakerCollection list)
	{
		CustomContext.getCurrentContext().setItem(SPEAKER_CACHE_KEY, list);
	}
	
	public static SessionStartDbAdapter getDbAdapter()
	{
		return CustomContext.getCurrentContext().getItem(ADAPTER_KEY);
	}
	
	public static void setDbAdapter(SessionStartDbAdapter adapter)
	{
		CustomContext.getCurrentContext().setItem(ADAPTER_KEY, adapter);
	}
	
	public static void setCurrentGrouping(GrouperBase grouping)
	{
		CustomContext.getCurrentContext().setItem("CurrentGrouping", grouping);
	}
	
	public static GrouperBase getCurrentGrouping()
	{
		GrouperBase result = CustomContext.getCurrentContext().getItem("CurrentGrouping");
		if (result == null)
			result = new SessionListGroupByDay();
		
		return result;
	}
	
	public static void setCurrentSessionKey(IGetKey getKey)
	{
		CustomContext.getCurrentContext().setItem("CurrentGetKey", getKey);
	}
	
	public static IGetKey getCurrentSessionKey()
	{
		IGetKey result = CustomContext.getCurrentContext().getItem("CurrentGetKey");
		if (result == null)
			result = new SessionKeyForDate();
		
		return result;
	}
	
	public static boolean getIsDataLoaded()
	{
		SessionCollection sessions = CustomContext.getCurrentContext().getItem(SESSION_CACHE_KEY);
		SpeakerCollection speakers = CustomContext.getCurrentContext().getItem(SPEAKER_CACHE_KEY);
		
		return sessions != null && speakers != null;
	}
}
